const axios = require("axios");
const { apiHost } = require("../config");
const mysql = require('../mysql/db')
/**
 * 响应 GET 请求（响应微信配置时的签名检查请求）
 */
async function list(ctx, next) {

  const { q,page=1,size=20 } = ctx.request.query
  let data = []
  if (q) {
    data = await mysql('poems').select('*')
      .where('title', 'like', `%${q}%`)
      .orWhere('content', 'like', `%${q}%`)
      .orWhere('author', 'like', `%${q}%`)
      .limit(size).offset((page - 1) * size)
  } else {
    data = await mysql('poems').select('*')
      .limit(size).offset((page - 1) * size)
  }

  ctx.body = data;
}

async function findById(ctx, next) {
  const { id } = ctx.params;
  const data = await mysql('poems').select('*').where({ id: parseInt(id) })
  const one = data[0]
  one.content = one.content.split('\n');
  ctx.body = one;
}

module.exports = {
  findById,
  list,
};
